package Test;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Properties;

public class  MailUtils {
   private static final String HOST="smtp.263.net";
   private static final String USER="oacs@auto-link.com.cn";
   private static final String PWD="Weaver@2001";
    /**
     * 创建邮件消息
     * @return 创建的邮件消息
     */
    private static MimeMessage createMail(){

        try {
            Properties properties = new Properties();
            properties.put("mail.smtp.auth","true");
            properties.put("mail.smtp.host", HOST);
            properties.put("mail.user",USER);
            properties.put("mail.password",PWD);
            // 构建授权信息，用于进行SMTP进行身份验证
            Authenticator authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(USER, PWD);
                }
            };
            // 使用环境属性和授权信息，创建邮件会话
            Session mailSession = Session.getInstance(properties, authenticator);
            // 创建邮件消息
            return new MimeMessage(mailSession);
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 发送一个简单的文本邮件
     * @param to 收件人邮箱
     * @param title 邮件标题
     * @param text  邮件内容
     * @return
     */
    public static boolean sendMail(String to, String title, String text, String filePath ){
        MimeMessage message = createMail();
        if (message==null){
            return false;
        }
        try {
            // 设置发件人
            InternetAddress form = new InternetAddress(USER);
            message.setFrom(form);

            // 设置收件人
            InternetAddress toAddress = new InternetAddress(to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // 设置邮件标题
            message.setSubject(title);

            // 设置邮件的内容体
            MimeMultipart mimeMultipart = new MimeMultipart("mixed");
            //message.setContent(text, "text/html;charset=UTF-8");
            message.setContent(mimeMultipart);
            ////指定为混合关系
            MimeBodyPart htmlPart = new MimeBodyPart();
            htmlPart.setContent(text,"text/html;charset=UTF-8");
            mimeMultipart.addBodyPart(htmlPart);
            //发送附件
            MimeBodyPart file = new MimeBodyPart();
            FileDataSource file_datasource = new FileDataSource(filePath);
            DataHandler dh = new DataHandler(file_datasource);
            file.setDataHandler(dh);
            //附件区别内嵌内容的一个特点是有文件名，为防止中文乱码要编码
            file.setFileName(MimeUtility.encodeText(dh.getName()));
            mimeMultipart.addBodyPart(file);
            // 发送邮件
            Transport.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) {
        String email = "826224501@qq.com";
        String title = "测试邮件";
        String url = "https://www.hao123.com/?src=from_pc";
        String msg = "【xxx】:\n</br>  尊敬的 " + "test"
                + " 您好,您在xxxx的密码即将过期，请及时<a href ='" + url + "'>点此登录xxx</a>修改密码，"
                + "以免影响正常使用。";
        String filePath = "/Users/dusongsong/Documents/上海泛微/数据字典/工作流程/workflow_requestlog(工作流请求签字日志表).html";
        boolean b = MailUtils.sendMail(email, title, msg,filePath);
        System.out.println("发送成功"+b);
    }
}
